﻿using UnityEngine;
using System.Collections;

public class MasterPassScene : MonoBehaviour
{
	// Constants

	const string		c_password	= "SHUTDOWN";


	// Properties

	[SerializeField]
	GameObject			m_masterPassPage;
	[SerializeField]
	GameObject			m_passFailedPage;
	[SerializeField]
	GameObject			m_passAcceptedPage;

	[SerializeField]
	SimpleTextInput		m_passInput;



	// Members

	enum Page
	{
		Password,
		Failed,
		Success,
	}

	Page	m_currentPage;


	void Awake()
	{
		m_currentPage			= Page.Password;
		//Debug.Log (m_passInput);
		m_passInput.Committed	+= OnPasswordCommit;
	}

	void Update()
	{
		if(m_currentPage != Page.Password)				// * Password 페이지가 아닐 경우 엔터 입력을 처리해야한다
		{
			if(Input.GetKeyDown(KeyCode.Return))
			{
				PanelConfirm();
			}
		}
		else
		{												// * Password 패널 상태에서는 ESC키에 반응해야함
			if(Input.GetKeyDown(KeyCode.Escape))
			{
				Application.LoadLevel("IntroScene");
			}
		}
	}

	void PanelConfirm()
	{
		if(m_currentPage == Page.Failed)				// * 실패 - 다시 패스워드 화면으로
		{
			ChangePage(Page.Password);
		}
		else if(m_currentPage == Page.Success)			// * 성공 - 메인메뉴 씬으로
		{
			Application.LoadLevel("IntroScene");
		}
	}


	/// <summary>
	/// 패스워드 체크
	/// </summary>
	/// <param name="password">Password.</param>
	void OnPasswordCommit(string password)
	{
		if(m_currentPage != Page.Password)
			return;

		bool correct	= (password == c_password);
		Page pageTo		= correct? Page.Success : Page.Failed;
		CodexManager.masterPass	= correct;

		ChangePage(pageTo);
	}


	/// <summary>
	/// UI 페이지 바꾸기
	/// </summary>
	/// <param name="pageTo">Page to.</param>
	void ChangePage(Page pageTo)
	{
		m_masterPassPage.SetActive(false);
		m_passFailedPage.SetActive(false);
		m_passAcceptedPage.SetActive(false);

		switch(pageTo)
		{
		case Page.Password:
			m_masterPassPage.SetActive(true);
			break;

		case Page.Failed:
			m_passInput.Clear();
			m_passFailedPage.SetActive(true);
			break;

		case Page.Success:
			m_passAcceptedPage.SetActive(true);
			break;
		}

		m_currentPage	= pageTo;
	}
}
